package controle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.ParseException;

import javax.swing.JOptionPane;

import abstraction.Note;

import presentation.NoteDialog;

import abstraction.Cours;

/** controleur qui ajoute une note
 * 
 * @author PICOSSON
 *
 */
public class ControlJButtonSaveNote implements ActionListener {
	
	private Cours modele;
	private NoteDialog parent;
	private boolean creation;
	
	public ControlJButtonSaveNote(NoteDialog parent, Cours modele, boolean creation){
		this.modele = modele;
		this.parent = parent;
		this.creation = creation;
	}

	public void actionPerformed(ActionEvent e) {
		Note note_saisie = null;
		note_saisie = new Note(modele.getEvaluationCourante(),
				parent.getEmail(), parent.getNote(),
				parent.getDate_correction(), parent.getCorrecteur(),
				parent.estDefinitive());

		
		try {
			if (note_saisie.getNote() < 0)
				throw new Exception("La note est négative.");
			
			if (note_saisie.getCorrecteur().length() < 1)
				throw new Exception("Il n'y a pas de correcteur");			
			
			if(!this.parent.getDate_correction().matches("^(19|20)\\d\\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$"))
				throw new Exception("La date est invalide.");			

			modele.saveNote(note_saisie, creation);
			parent.setVisible(false);
		} catch (Exception e1) {			
			JOptionPane.showMessageDialog(parent, "Impossible! " + e1.getMessage(), "Erreur!",
					JOptionPane.ERROR_MESSAGE);
		}
	}
}
	

	


